package com.qst.system.service.impl;

import com.qst.system.domain.vo.MaterialsVo;
import com.qst.system.mapper.MaterialsMapper;
import com.qst.system.service.IMaterialsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class MaterialsServiceImpl implements IMaterialsService {

    @Autowired
    private MaterialsMapper materialsMapper;

    @Override
    public List<MaterialsVo> selectMaterialsList(MaterialsVo materials) {
        List<MaterialsVo> materialsVos = materialsMapper.selectMaterialsList(materials);
        return materialsVos;
    }

    @Override
    public MaterialsVo selectMaterialsById(Long lisId) {
        return materialsMapper.selectMaterialsById(lisId);
    }

    @Override
    public int insertMaterials(MaterialsVo materials) {
        // 根据企业名称查找企业ID
        if (materials.getComName() != null && !materials.getComName().isEmpty()) {
            Long comId = materialsMapper.selectCompanyIdByName(materials.getComName());
            if (comId != null) {
                materials.setLisComId(comId);
            }
        }
        
        // 根据贷款编号查找贷款ID
        if (materials.getLoaNo() != null && !materials.getLoaNo().isEmpty()) {
            Long loaId = materialsMapper.selectLoanIdByNo(materials.getLoaNo());
            if (loaId != null) {
                materials.setLisLoaId(loaId);
            }
        }
        
        return materialsMapper.insertMaterials(materials);
    }

    @Override
    public int updateMaterials(MaterialsVo materials) {
        // 根据企业名称查找企业ID
        if (materials.getComName() != null && !materials.getComName().isEmpty()) {
            Long comId = materialsMapper.selectCompanyIdByName(materials.getComName());
            if (comId != null) {
                materials.setLisComId(comId);
            }
        }
        
        // 根据贷款编号查找贷款ID
        if (materials.getLoaNo() != null && !materials.getLoaNo().isEmpty()) {
            Long loaId = materialsMapper.selectLoanIdByNo(materials.getLoaNo());
            if (loaId != null) {
                materials.setLisLoaId(loaId);
            }
        }
        
        return materialsMapper.updateMaterials(materials);
    }

    @Override
    public int deleteMaterialsById(Long lisId) {
        return materialsMapper.deleteMaterialsById(lisId);
    }

    @Override
    public int deleteMaterialsByIds(Long[] lisIds) {
        return materialsMapper.deleteMaterialsByIds(lisIds);
    }
}